﻿using MassTransit.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Infrastructure.MessageBus
{
    public static class MessageBusQueueNameUtils
    {
        internal static string QueueNamePrifx;
        static Dictionary<int, Tuple<int, string>> Map = new Dictionary<int, Tuple<int, string>>();

        //private static void InitMap()
        //{
        //    if (Map.Count == 0)
        //    {
        //        lock (Map)
        //        {
        //            if (Map.Count == 0)
        //            {
        //                for (char i = (Char)65; i < 90; i++)
        //                {
        //                    var key = (int)i;
        //                    String queueName = $"{QueueNamePrifx}.{(char)i}{(char)(++i)}";                            

        //                    Map.Add(key, Tuple.Create(new Range(key, key+1), queueName));
        //                    Map.Add(key + 1, Tuple.Create(new Range(key, key + 1), queueName));
        //                }

        //                Map.Add(0, Tuple.Create(new Range(0, 65), $"{QueueNamePrifx}.None"));
        //                Map.Add(91, Tuple.Create(new Range(91, 127), $"{QueueNamePrifx}.{(char)91}"));
        //            }
        //        }
        //    }
        //}

        private static void InitMapV1()
        {
            if (Map.Count == 0)
            {
                lock (Map)
                {
                    if (Map.Count == 0)
                    {
                        for (int i = 0; i < 16; i++)
                        {
                            var key = i;
                            String queueName = $"{QueueNamePrifx}.{i}";

                            Map.Add(key, Tuple.Create(key, queueName));
                        }
                    }
                }
            }
        }

        public static List<Tuple<int, string>> GetQueueName()
        {
            InitMapV1();

            List<Tuple<int, string>> vs = Map.Values.ToList();

            return vs;
        }

        public static Tuple<int, string> GetQueueName(string ClassName)
        {
            var index = (int)(ClassName.ToUpper().First())%16;

            InitMapV1();


            return Map[index];
        }
    }
}
